package com.gdcontrol.desktop.visao.manutencao;

import com.gdcontrol.desktop.controle.manutencao.ControleManutencaoPrescricao;
import com.gdcontrol.desktop.visao.consulta.VisaoConsultaMedicacao;
import com.gdcontrol.desktop.visao.consulta.VisaoConsultaMedico;
import com.gdcontrol.entidade.Medicacao;
import com.gdcontrol.entidade.Medico;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author gustavo
 */
public class VisaoManutencaoPrescricao extends VisaoManutencaoPadrao {

    /**
     * Creates new form VisaoManutencaoPrescricao
     */
    public VisaoManutencaoPrescricao(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.addComponenteTexto(edId);
        this.addComponenteTexto(edDataFim);
        this.addComponenteTexto(edDataInicio);
        this.addComponenteTexto(edFatorCarboidrato);
        this.addComponenteTexto(edFatorCorrecao);
        this.addComponenteTexto(edFatorSensibilidade);
        this.addComponenteTexto(edMinimoGlicemia);
        this.addComponenteTexto(edMaximoGlicemia);
        this.addComponenteTexto(edIdMedicacao);
        this.addComponenteTexto(edNomeMedicacao);
        this.addComponenteTexto(edIdMedico);
        this.addComponenteTexto(edNomeMedico);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        edId = new javax.swing.JTextField();
        edDataInicio = new javax.swing.JFormattedTextField();
        edDataFim = new javax.swing.JFormattedTextField();
        edFatorCarboidrato = new javax.swing.JTextField();
        edFatorCorrecao = new javax.swing.JTextField();
        edFatorSensibilidade = new javax.swing.JTextField();
        edIdMedico = new javax.swing.JTextField();
        edNomeMedico = new javax.swing.JTextField();
        edIdMedicacao = new javax.swing.JTextField();
        edNomeMedicacao = new javax.swing.JTextField();
        btConfirmar = new javax.swing.JButton();
        btLimpar = new javax.swing.JButton();
        btPesquisarMedico = new javax.swing.JButton();
        btPesquisarMedicacao = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        edMinimoGlicemia = new javax.swing.JTextField();
        edMaximoGlicemia = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Prescrição");

        jLabel1.setText("ID:");

        jLabel2.setText("Data Início:");

        jLabel3.setText("Data Fim:");

        jLabel4.setText("Fator Carboidrato:");

        jLabel5.setText("Fator Correção:");

        jLabel6.setText("Fator Sensibilidade:");

        jLabel7.setText("Medicação:");

        jLabel8.setText("Médico:");

        edId.setEnabled(false);

        edDataInicio.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter()));

        edDataFim.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter()));

        edIdMedico.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                edIdMedicoFocusLost(evt);
            }
        });

        edIdMedicacao.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                edIdMedicacaoFocusLost(evt);
            }
        });

        btConfirmar.setText("Confirmar");
        btConfirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btConfirmarActionPerformed(evt);
            }
        });

        btLimpar.setText("Limpar");
        btLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLimparActionPerformed(evt);
            }
        });

        btPesquisarMedico.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/gdcontrol/desktop/util/img/pesquisar.png"))); // NOI18N
        btPesquisarMedico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisarMedicoActionPerformed(evt);
            }
        });

        btPesquisarMedicacao.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/gdcontrol/desktop/util/img/pesquisar.png"))); // NOI18N
        btPesquisarMedicacao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisarMedicacaoActionPerformed(evt);
            }
        });

        jLabel9.setText("Mínimo Glicemia:");

        jLabel10.setText("Máximo Glicemia:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel6)
                                        .addComponent(jLabel5)
                                        .addComponent(jLabel4)
                                        .addComponent(jLabel3)
                                        .addComponent(jLabel2)
                                        .addComponent(jLabel1)
                                        .addComponent(jLabel9))
                                    .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(edId, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(edDataInicio, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                                        .addComponent(edDataFim))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(edMaximoGlicemia, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(edMinimoGlicemia, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(edFatorSensibilidade, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
                                        .addComponent(edFatorCorrecao, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(edFatorCarboidrato, javax.swing.GroupLayout.Alignment.LEADING))))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btConfirmar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btLimpar))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(48, 48, 48)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(edIdMedicacao, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(edIdMedico, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(edNomeMedico)
                            .addComponent(edNomeMedicacao, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btPesquisarMedico, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btPesquisarMedicacao, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(67, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(edId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(edDataInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(edDataFim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(edFatorCarboidrato, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(edFatorCorrecao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(edFatorSensibilidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(edMinimoGlicemia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(edMaximoGlicemia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btPesquisarMedico, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel8)
                        .addComponent(edIdMedico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(edNomeMedico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(edIdMedicacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(edNomeMedicacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btPesquisarMedicacao, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btConfirmar)
                    .addComponent(btLimpar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLimparActionPerformed
        this.limpar();
    }//GEN-LAST:event_btLimparActionPerformed

    private void btConfirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btConfirmarActionPerformed
        this.controle.setTela(this);
        this.controle.salvar();
        dispose();
    }//GEN-LAST:event_btConfirmarActionPerformed

    private void edIdMedicoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_edIdMedicoFocusLost
        if(!this.getEdIdMedico().getText().isEmpty()){
            Medico med = getControle().buscaMedico(Integer.parseInt(this.getEdIdMedico().getText()));
            if(med != null){
                this.getEdNomeMedico().setText(med.getNome());
            }else{
                this.getEdIdMedico().setText(null);
                this.getEdNomeMedico().setText(null);
                JOptionPane.showMessageDialog(rootPane, "Não foi encontrado Médico com o ID solicitado!");
            }
        }
    }//GEN-LAST:event_edIdMedicoFocusLost

    private void edIdMedicacaoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_edIdMedicacaoFocusLost
        if(!this.getEdIdMedicacao().getText().isEmpty()){
            Medicacao med = getControle().buscaMedicacao(Integer.parseInt(this.getEdIdMedicacao().getText()));
            if(med != null){
                this.getEdNomeMedicacao().setText(med.getNomeComercial());
            }else{
                this.getEdIdMedicacao().setText(null);
                this.getEdNomeMedicacao().setText(null);
                JOptionPane.showMessageDialog(rootPane, "Não foi encontrada Medicação com o ID solicitado!");
            }
        }
    }//GEN-LAST:event_edIdMedicacaoFocusLost

    private void btPesquisarMedicoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisarMedicoActionPerformed
        VisaoConsultaMedico consultaMedico = new VisaoConsultaMedico(null, true);
        consultaMedico.setVisible(true);
    }//GEN-LAST:event_btPesquisarMedicoActionPerformed

    private void btPesquisarMedicacaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisarMedicacaoActionPerformed
        VisaoConsultaMedicacao consultaMedicacao = new VisaoConsultaMedicacao(null, true);
        consultaMedicacao.setVisible(true);
    }//GEN-LAST:event_btPesquisarMedicacaoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VisaoManutencaoPrescricao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VisaoManutencaoPrescricao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VisaoManutencaoPrescricao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VisaoManutencaoPrescricao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                VisaoManutencaoPrescricao dialog = new VisaoManutencaoPrescricao(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btConfirmar;
    private javax.swing.JButton btLimpar;
    private javax.swing.JButton btPesquisarMedicacao;
    private javax.swing.JButton btPesquisarMedico;
    private javax.swing.JFormattedTextField edDataFim;
    private javax.swing.JFormattedTextField edDataInicio;
    private javax.swing.JTextField edFatorCarboidrato;
    private javax.swing.JTextField edFatorCorrecao;
    private javax.swing.JTextField edFatorSensibilidade;
    private javax.swing.JTextField edId;
    private javax.swing.JTextField edIdMedicacao;
    private javax.swing.JTextField edIdMedico;
    private javax.swing.JTextField edMaximoGlicemia;
    private javax.swing.JTextField edMinimoGlicemia;
    private javax.swing.JTextField edNomeMedicacao;
    private javax.swing.JTextField edNomeMedico;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    // End of variables declaration//GEN-END:variables

    ControleManutencaoPrescricao controle = new ControleManutencaoPrescricao();
    
    @Override
    public ControleManutencaoPrescricao getControle() {
        return this.controle;
    }

    public JTextField getEdNomeMedicacao() {
        return edNomeMedicacao;
    }

    public JFormattedTextField getEdDataFim() {
        return edDataFim;
    }

    public JFormattedTextField getEdDataInicio() {
        return edDataInicio;
    }

    public JTextField getEdNomeMedico() {
        return edNomeMedico;
    }

    public JTextField getEdFatorCarboidrato() {
        return edFatorCarboidrato;
    }

    public JTextField getEdFatorCorrecao() {
        return edFatorCorrecao;
    }

    public JTextField getEdFatorSensibilidade() {
        return edFatorSensibilidade;
    }

    public JTextField getEdId() {
        return edId;
    }

    public JTextField getEdIdMedicacao() {
        return edIdMedicacao;
    }

    public JTextField getEdIdMedico() {
        return edIdMedico;
    }    

    public JTextField getEdMaximoGlicemia() {
        return edMaximoGlicemia;
    }

    public void setEdMaximoGlicemia(JTextField edMaximoGlicemia) {
        this.edMaximoGlicemia = edMaximoGlicemia;
    }

    public JTextField getEdMinimoGlicemia() {
        return edMinimoGlicemia;
    }

    public void setEdMinimoGlicemia(JTextField edMinimoGlicemia) {
        this.edMinimoGlicemia = edMinimoGlicemia;
    }   
}